package com.atguigu.gmall.mq.service;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.entity.GmallCorrelationData;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 封装发送消息  MQ
 */
@Component
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    //发消息（普通）
    public void sendMessage(String exchange,String routingKey,Object msg){
        //封装交换机 应答返回对象
        GmallCorrelationData correlationData = new GmallCorrelationData();
        //主键
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        correlationData.setId(id);
        System.out.println("发送消息的时候的主键：=="+id);
        //交换机
        correlationData.setExchange(exchange);
        //RoutingKey
        correlationData.setRoutingKey(routingKey);
        //消息体
        correlationData.setMessage(msg);
        /*
        为防止队列接收消息失败，将上面的GmallCorrelationData 保存在Redis缓存中 Key：UUID主键  V：GmallCorrelationData

         */
        redisTemplate.opsForValue().set(id, JSONObject.toJSONString(correlationData),5, TimeUnit.MINUTES);
        //发消息
        rabbitTemplate.convertAndSend(exchange,routingKey,msg,correlationData);
    }

    //基于插件的发送延迟消息的封装
    public void sendDelayedMessage(String exchange, String routingKey, Object msg, int delayTime) {
        //封装交换机 应答返回对象
        GmallCorrelationData correlationData = new GmallCorrelationData();
        //主键
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        correlationData.setId(id);
        System.out.println("发送延迟消息的时候的主键：==》"+id);
        //1.交换机
        correlationData.setExchange(exchange);
        //2.RoutingKey
        correlationData.setRoutingKey(routingKey);
        //3.消息体
        correlationData.setMessage(msg);
        //4.设置是否延迟
        correlationData.setDelay(true);
        //5.设置延迟时间
        correlationData.setDelayTime(delayTime);
        //为了防止 队列接收消息失败 应答的时候 需要使用到下面缓存信息
        redisTemplate.opsForValue().set(id, JSONObject.toJSONString(correlationData), 5, TimeUnit.MINUTES);
        //发消息 设置延迟的时间
        rabbitTemplate.convertAndSend(exchange, routingKey,
                msg, (message) -> {
                    message.getMessageProperties().setDelay(correlationData.getDelayTime());
                    System.out.println("发送延迟消息的时间：" + new Date());
                    return message;
                }, correlationData);
    }
}
